<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="选择区域" prop="region">
        <el-cascader
          v-model="queryParams.region"
          :options="regionOptionsAll"
          :props="{ value:'label',label:'label'}"
          clearable></el-cascader>
      </el-form-item>
      <el-form-item label="学校名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入学校名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="classify">
        <el-select v-model="queryParams.classify" placeholder="学校类型" clearable>
          <el-option
            v-for="dict in dict.type.sys_school_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:notice:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tableData" >
      <el-table-column type="index" align="center" width="80"></el-table-column>
      <el-table-column label="学校名称" align="center" prop="name" :show-overflow-tooltip="true"/>
      <el-table-column label="学校类型" align="center" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_school_type" :value="scope.row.classify"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)">修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新建学校对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="480px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="22">
            <el-form-item label="选择地区" prop="region">
              <el-cascader 
                style="width: 100%;"
                v-model="form.region"
                :props="{value:'label',label:'label'}"
                :options="regionOptions"
                clearable></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="学校名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入学校名称" />
            </el-form-item>
          </el-col>
         <el-col :span="22">
            <el-form-item label="学校类型" prop="classify">
              <el-select style="width: 100%" v-model="form.classify" placeholder="学校类型" clearable>
                <el-option
                  v-for="dict in dict.type.sys_school_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSchoolByPage, addSchool, delSchool } from "@/api/school/school.js";
import { regionData, regionDataAllOption } from '@/config/region-shandong.js'

export default {
  name: "SchoolList",
  dicts: ['sys_school_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //地域数组
      regionOptions: regionData,
      regionOptionsAll: regionDataAllOption,
      // 公告表格数据
      tableData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        region: ['青岛市', '全部'],
        name: undefined,
        classify: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        region: [
          { required: true, message: "地域不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "名称不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询学校列表 */
    getList() {
      this.loading = true;
      let region = this.queryParams.region;
      // console.log(region)
     
      this.queryParams.province = '山东省';
      this.queryParams.city = region[0]||'';
      this.queryParams.district = (region[1]||'').replace('全部', '');
      // console.log(this.queryParams)
      listSchoolByPage(this.queryParams).then(response => {
        this.tableData = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        region: [],
        name: "",
        classify: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新建学校";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = Object.assign({}, this.form, {
        id: row.id,
        name: row.name,
        region: [row.city, row.district],
        classify: row.classify+""
      })
      this.open = true;
      this.title = "编辑学校";
    },
    /** 新建学校-提交 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let region = this.form.region;
          this.form.province = '山东省';
          this.form.city = region[0];
          this.form.district = region[1];
          // console.log(this.form)
          addSchool(this.form).then(response => {
            this.$modal.msgSuccess("提交成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id
      this.$modal.confirm('是否确认删除学校编号为"' + id + '"的数据项？').then(function() {
        return delSchool({id});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
